package com.gxa.pingan.controller;

import com.gxa.pingan.common.JsonResult;
import com.gxa.pingan.exception.BusinessException;
import com.gxa.pingan.pojo.InsCompany;
import com.gxa.pingan.service.InsCompanyService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @author lijianmin
 * @date 2021/10/25  20:22:25
 */
@CrossOrigin
@RestController
@RequestMapping("/insCompany")
@Api(value = "保险公司管理的接口", tags = "保险公司管理接口的API")
public class InsCompanyController {

    @Autowired
    private InsCompanyService insCompanyService;

    /**
     * 保险公司列表查询的方法
     *
     * @param page 页码
     * @param size 每页记录数
     * @return 返回查询到的集合
     */
    @GetMapping("/getList")
    @ApiOperation(value = "保险公司列表分页查询的方法")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "页码", paramType = "query", required = true,dataType = "Integer"),
            @ApiImplicitParam(name = "size", value = "每页记录数", paramType = "query", required = true,dataType = "Integer")
    })
    public Map getList(@RequestParam("page") Integer page,@RequestParam("size") Integer size) throws BusinessException {
        List<InsCompany> list = insCompanyService.getList(page, size);
        Integer listCount = insCompanyService.getListCount();
        JsonResult jsonResult = new JsonResult();
        if (listCount != 0) {
            jsonResult.setCode("1");
            jsonResult.setMsg("查询成功");
            jsonResult.setData(list);
            jsonResult.put("count", listCount);
        }else {
            jsonResult.setMsg("0");
            jsonResult.setMsg("当前无数据");
        }
        return jsonResult.getDataMap();
    }


    /**
     * 添加保险公司的方法
     *
     * @param insCompany 添加的保险公司对象
     * @return 返回是否成功
     */
    @PostMapping("/add")
    @ApiOperation(value = "添加保险公司的方法")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "insCompany", value = "添加的保险公司对象",paramType = "body",dataType = "InsCompany")
    })
    public Map add(@RequestBody InsCompany insCompany) throws BusinessException {
        Boolean add = insCompanyService.add(insCompany);
        JsonResult jsonResult = new JsonResult();
        if (add) {
            jsonResult.setCode("1");
            jsonResult.setMsg("添加成功");
        } else {
            jsonResult.setMsg("0");
            jsonResult.setMsg("添加失败");
        }
        return jsonResult.getDataMap();
    }

    /**
     * 修改保险公司的方法
     *
     * @param insCompany 修改的保险公司对象
     * @return 返回是否成功
     */
    @PostMapping("/update")
    @ApiOperation(value = "修改保险公司信息的方法")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "insCompany", value = "修改的保险公司对象", paramType = "body",dataType = "InsCompany")
    })
    public Map update(@RequestBody InsCompany insCompany) throws BusinessException {
        Boolean update = insCompanyService.update(insCompany);
        JsonResult jsonResult = new JsonResult();
        if (update) {
            jsonResult.setMsg("修改成功");
            jsonResult.setCode("1");
        } else {
            jsonResult.setMsg("修改失败");
            jsonResult.setCode("0");
        }
        return jsonResult.getDataMap();
    }

    /**
     * 删除保险公司的方法
     *
     * @param id 删除的保险公司的id
     * @return 返回是否成功
     */
    @PostMapping("/delete")
    @ApiOperation(value = "删除保险公司的方法")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "删除的保险公司的id",paramType = "query",dataType = "Long")
    })
    public Map delete(@RequestParam("id") Long id) throws BusinessException {
        Boolean delete = insCompanyService.delete(id);
        JsonResult jsonResult = new JsonResult();
        if (delete) {
            jsonResult.setMsg("删除成功");
            jsonResult.setCode("1");
        } else {
            jsonResult.setMsg("删除失败");
            jsonResult.setCode("0");
        }
        return jsonResult.getDataMap();
    }

    @PostMapping("/findAll")
    @ApiOperation("这是公司的全查询方法")
  public Map findAll(){
      List<InsCompany> all = insCompanyService.findAll();
      JsonResult jsonResult=new JsonResult();
      jsonResult.setCode("1");
      jsonResult.setData(all);
      return jsonResult.getDataMap();
  }

}
